#!/bin/sh

default_proxy=$(uci -q get fchomo.routing.default_proxy)
if [ -n "$default_proxy" ]; then
	uci -q batch <<-EOF >"/dev/null"
		delete fchomo.routing.default_proxy
		set fchomo.routing.client_enabled="1"
		set fchomo.autogened_final_host="rules"
		set fchomo.autogened_final_host.label="Auto Generated Final"
		set fchomo.autogened_final_host.entry="MATCH,$default_proxy"
	EOF
fi

for option in routing_tcpport routing_udpport; do
	value=$(uci -q get fchomo.routing.$option)
	if [ -z "$value" ]; then
		uci -q batch <<-EOF >"/dev/null"
			delete fchomo.routing.$option
			add_list fchomo.routing.$option="all"
		EOF
	elif [ "$value" = "common" ]; then
		uci -q batch <<-EOF >"/dev/null"
			delete fchomo.routing.$option
			add_list fchomo.routing.$option="${option/routing/common}"
		EOF
	elif [ "$value" = "common_stun" ]; then
		uci -q batch <<-EOF >"/dev/null"
			delete fchomo.routing.$option
			add_list fchomo.routing.$option="${option/routing/common}"
			add_list fchomo.routing.$option="stun_port"
		EOF
	fi
done

dns_port=$(uci -q get fchomo.dns.port)
if [ -n "$dns_port" ]; then
	uci -q batch <<-EOF >"/dev/null"
		delete fchomo.dns.port
		set fchomo.dns.dns_port="$dns_port"
	EOF
fi

uci commit fchomo

exit 0
